/**
 * Implementation for Morphlynx password checker.
 * 
 * Call to initialize: 
 * 	MORPHLYNX.PASSWORD_UTIL.init(...);
 */
try {
	MORPHLYNX;
} catch(e) {
	MORPHLYNX = {};
}

MORPHLYNX.PASSWORD_UTIL = (function(){
	var _passwordId = "",
		_isInitialized = false,
		_animationDelay = 200,
	
	_registerEvents = function() {
		$(document).on("keyup", _passwordId, function() {
			var passValue = $(this).val(), 
				score = 0;
			
			if (passValue.length >= 8) {
				$('#password-eight-characters').attr('class', 'pass');
				score += 2;
			} else {
				$('#password-eight-characters').attr('class', 'fail');				
			}
			
			if (passValue.match(/[A-Z]/g)) {
				$('#password-one-capital').attr('class', 'pass');
				score += 1;
			} else {
				$('#password-one-capital').attr('class', 'fail');				
			}

			if (passValue.match(/[0-9]/g)) {
				$('#password-one-number').attr('class', 'pass');
				score += 1;
			} else {
				$('#password-one-number').attr('class', 'fail');				
			}
			
			if (passValue.match(/[\!@\#\$\%\^\&\*]/g)) {
				$('#password-special-char').attr('class', 'pass');
				score += 1;
			} else {
				$('#password-special-char').attr('class', 'fail');				
			}
			
			if (score < 4) {
				$('#password-strength').attr('class','weak');
				$('#password-strength').html('Weak');
			} else if (score == 4) {
				$('#password-strength').attr('class','good');
				$('#password-strength').html('Good');				
			} else {				
				$('#password-strength').attr('class','strong');
				$('#password-strength').html('Strong');				
			}
		}).on("focusin", _passwordId, function() {
			$('#password-validation-wrapper').fadeIn(_animationDelay);
		}).on("focusout", _passwordId, function() {
			$('#password-validation-wrapper').fadeOut(_animationDelay);
		});
	};
	
	return {
		init : function(passwordId, settings) {
			if(_isInitialized)
				return; //already been initialized
			if(passwordId == null || passwordId.length == 0) {
				alert("Error initializing password validator!"); //should always have an ID
				return;
			}
			
			_isInitialized = true;
			_passwordId = "#" + passwordId;
			
			if(settings != null) {
				if(settings.animationDelay != null)
					_animationDelay = parseInt(settings.animationDelay);
			}
			
			_registerEvents();
		}
	};
})();